package com.example.server.service.impl;

import com.example.server.mapper.MyEmployeeMapper;
import com.example.server.pojo.MyEmployee;
import com.example.server.pojo.PageParam;
import com.example.server.service.MyEmployeeService;
import com.example.server.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class MyEmployeeServiceImpl implements MyEmployeeService {
    @Autowired
    private MyEmployeeMapper myEmployeeMapper;

    @Override
    public List<MyEmployee> allEmp() {
        List<MyEmployee> myEmployeeList =  myEmployeeMapper.allEmp();
        return myEmployeeList;
    }

    @Override
    public Map pageEmp(PageParam pageParam) {
        return PageUtils.getPageResult(getPageInfo(pageParam));
    }

    public PageInfo<MyEmployee> getPageInfo(PageParam pageParam) {
        Integer currentPageParam = pageParam.getPagenum();
        Integer pageSizeParam = pageParam.getPagesize();
        int currenPage = 1;
        int pageSize = 5;
        if (StringUtils.isEmpty(currentPageParam)) {
            currenPage = 1;
        } else {
            currenPage = currentPageParam;
        }
        if (StringUtils.isEmpty(pageSizeParam)) {
            pageSize = 5;
        } else {
            pageSize = pageSizeParam;
        }
        //分页条件查询参数
//        PageHelper.startPage(currenPage, pageSize);//页数和每页大小
//        List<MyEmployee> myEmployeeList = myEmployeeMapper.allEmpPage();
//        PageInfo<MyEmployee> pageInfo = new PageInfo<MyEmployee>(myEmployeeList);
        // 另一种写法，对集合查询进行分页，需要查询所有集合
        PageInfo<MyEmployee> pageInfo = PageHelper.startPage(currenPage, pageSize).doSelectPageInfo(() -> allEmp());
        return pageInfo;
    }


    public Map allEmpPage(PageParam pageParam) {
        Integer currentPageParam = pageParam.getPagenum();
        Integer pageSizeParam   =  pageParam.getPagesize();

        int currenPage = 1;
        int pageSize = 5;
        if(StringUtils.isEmpty(currentPageParam)){
            currenPage = 1;
        }else{
            currenPage = currentPageParam;
        }
        if(StringUtils.isEmpty(pageSizeParam)){
            pageSize = 5;
        }else{
            pageSize = pageSizeParam;

        }
        //分页条件查询参数
        PageHelper.startPage(currenPage,pageSize);//页数和每页大小
        List<MyEmployee> myEmployeeList =  myEmployeeMapper.allEmpPage();
        PageInfo<MyEmployee> pageInfo = new PageInfo<MyEmployee>(myEmployeeList);

        List<MyEmployee> myEmployees = new ArrayList<>();
        if(!CollectionUtils.isEmpty(myEmployeeList)){
            for(MyEmployee myEmployee:pageInfo.getList()){
                myEmployees.add(myEmployee);
            }
        }

        Map<String,Object> map = new HashMap<>();
        map.put("total",pageInfo.getTotal());
        map.put("currentPage",pageInfo.getPageNum());
        map.put("pageSize", pageInfo.getPageSize());

        Map<String ,Object> maps = new HashMap<>();
        maps.put("pageInfo",map);
        maps.put("MyEmployee",myEmployees);
        return maps;
    }

    @Override
    public List<MyEmployee> querylikeEmp(String name) {
        return myEmployeeMapper.querylikeEmp(name);
    }

    @Override
    public MyEmployee selectOneEmp(Long id) {
        return myEmployeeMapper.selectOneEmp(id);
    }

    @Override
    public Integer addEmp(MyEmployee myEmployee) {
        return myEmployeeMapper.addEmp(myEmployee);
    }

    @Override
    public Integer updateEmp(MyEmployee myEmployee) {
        return myEmployeeMapper.updateEmp(myEmployee);
    }

    @Override
    public Integer delEmp(Long id) {
        return myEmployeeMapper.delEmp(id);
    }
}
